package cn.edu.scau.cmi.huangxiyan.client;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import cn.edu.scau.cmi.huangxiyan.composite.SafeComponent;
import cn.edu.scau.cmi.huangxiyan.composite.SafeComposite;
import cn.edu.scau.cmi.huangxiyan.composite.SafeLeaf;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SafeCompositeClient extends Application {

	public Set<SafeComponent> safeLeaf = new HashSet<SafeComponent>();
	public Set<SafeComponent> safeComposite = new HashSet<SafeComponent>();
	public TextArea textArea = new TextArea("");

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("SafeCompositeClient");
		VBox vBox = new VBox(15);
		vBox.setPadding(new Insets(15, 5, 5, 5));

		Label personLabel = new Label("个人姓名：");
		personLabel.setPrefSize(80, 25);

		TextField personTextField = new TextField("请输入个人姓名");
		personTextField.setPrefSize(520, 25);

		HBox personHBox = new HBox(5);
		personHBox.setPadding(new Insets(5, 0, 5, 0));
		personHBox.getChildren().addAll(personLabel, personTextField);

		Label teamLabel = new Label("团体1名称：");
		teamLabel.setPrefSize(80, 25);

		TextField teamTextField = new TextField("请输入团体1名称");
		teamTextField.setPrefSize(520, 25);

		HBox teamHBox = new HBox(5);
		teamHBox.setPadding(new Insets(5, 0, 5, 0));
		teamHBox.getChildren().addAll(teamLabel, teamTextField);

		Label team2Label = new Label("团体2名称：");
		team2Label.setPrefSize(80, 25);

		TextField team2TextField = new TextField("请输入团体2名称");
		team2TextField.setPrefSize(520, 25);

		HBox team2HBox = new HBox(5);
		team2HBox.setPadding(new Insets(5, 0, 5, 0));
		team2HBox.getChildren().addAll(team2Label, team2TextField);

		textArea.setPrefSize(600, 200);
		textArea.setEditable(false);

		Button viewAllButton = new Button("显示全部");
		viewAllButton.setPrefSize(300, 25);
		viewAllButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				for (SafeComponent c : safeComposite) {
					if (((SafeComposite) c).getParent() == null) {
						viewComposite(c, 1);
					}
				}

				for (SafeComponent c : safeLeaf) {
					if (((SafeLeaf) c).getParent() == null) {
						textArea.appendText("-个人：" + ((SafeLeaf) c).getName() + "\n");
					}
				}

				if (textArea.getText().equals("")) {
					textArea.appendText("无任何个人或团队！\n");
				}
			}
		});

		Button createPersonButton = new Button("创建个人");
		createPersonButton.setPrefSize(300, 25);
		createPersonButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				SafeLeaf leaf = new SafeLeaf();
				leaf.setName(personTextField.getText());

				if (safeLeaf.contains(leaf)) {
					textArea.appendText("个人 " + leaf.getName() + " 已存在，无法创建！");
				} else {
					safeLeaf.add(leaf);
					textArea.appendText("创建个人 " + leaf.getName() + " 成功！");
				}
			}
		});

		Button createTeamButton = new Button("创建团队1");
		createTeamButton.setPrefSize(300, 25);
		createTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				SafeComposite composite = new SafeComposite();
				composite.setName(teamTextField.getText());

				if (safeComposite.contains(composite)) {
					textArea.appendText("团队1 " + composite.getName() + " 已存在，无法创建！");
				} else {
					safeComposite.add(composite);
					textArea.appendText("创建团队1 " + composite.getName() + " 成功！");
				}
			}
		});

		Button addPersonToTeamButton = new Button("添加个人到团队1");
		addPersonToTeamButton.setPrefSize(150, 25);
		addPersonToTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				SafeComposite composite = new SafeComposite();
				composite.setName(teamTextField.getText());
				Iterator<SafeComponent> iterator = safeComposite.iterator();
				while (iterator.hasNext()) {
					SafeComposite temp = (SafeComposite) iterator.next();
					if (temp.getName().equals(composite.getName())) {
						composite = temp;
						break;
					}
				}

				SafeLeaf leaf = new SafeLeaf();
				leaf.setName(personTextField.getText());
				iterator = safeLeaf.iterator();
				while (iterator.hasNext()) {
					SafeLeaf temp = (SafeLeaf) iterator.next();
					if (temp.getName().equals(leaf.getName())) {
						leaf = temp;
						break;
					}
				}

				if (safeComposite.contains(composite)) {
					if (safeLeaf.contains(leaf)) {
						if (leaf.getParent() == null) {
							safeComposite.remove(composite);
							composite.addChild(leaf);
							safeComposite.add(composite);
							safeLeaf.remove(leaf);
							leaf.setParent(composite);
							safeLeaf.add(leaf);
							textArea.appendText("添加个人 " + leaf.getName() + " 到团队1 " + composite.getName() + " 成功！");
						} else if (leaf.getParent().getName().equals(composite.getName())) {
							textArea.appendText("个人 " + leaf.getName() + " 已加入团队1 " + composite.getName() + " ，无法添加！");
						} else {
							textArea.appendText("个人 " + leaf.getName() + " 已加入其它团队 " + composite.getName() + " ，无法添加！");
						}
					} else {
						textArea.appendText("个人 " + leaf.getName() + " 不存在，请先创建再进行添加操作！");
					}
				} else {
					textArea.appendText("团队1 " + composite.getName() + " 不存在，请先创建再进行添加操作！");
				}
			}
		});

		Button addTeamToTeamButton = new Button("添加团队1到团队2");
		addTeamToTeamButton.setPrefSize(150, 25);
		addTeamToTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				if (!(team2TextField.getText().equals(teamTextField.getText()))) {
					SafeComposite composite2 = new SafeComposite();
					composite2.setName(team2TextField.getText());
					SafeComposite composite = new SafeComposite();
					composite.setName(teamTextField.getText());
					int flag2 = 0, flag = 0;
					Iterator<SafeComponent> iterator = safeComposite.iterator();
					while (iterator.hasNext() && (flag2 == 0 || flag == 0)) {
						SafeComposite temp = (SafeComposite) iterator.next();
						if (temp.getName().equals(composite2.getName())) {
							composite2 = temp;
							flag2 = 1;
						} else if (temp.getName().equals(composite.getName())) {
							composite = temp;
							flag = 1;
						}
					}

					if (safeComposite.contains(composite2)) {
						if (safeComposite.contains(composite)) {
							if (composite.getParent() == null) {
								safeComposite.remove(composite2);
								composite2.addChild(composite);
								safeComposite.add(composite2);
								safeComposite.remove(composite);
								composite.setParent(composite2);
								safeComposite.add(composite);

								textArea.appendText(
										"添加团队1  " + composite.getName() + " 到团队2 " + composite2.getName() + " 成功！");
							} else if (composite.getParent().getName().equals(composite2.getName())) {
								textArea.appendText(
										"团队1 " + composite.getName() + " 已加入团队2 " + composite2.getName() + " ，无法添加！");
							} else {
								textArea.appendText("团队1 " + composite.getName() + " 已加入其它团队 "
										+ composite.getParent().getName() + " ，无法添加！");
							}
						} else {
							textArea.appendText("团队1 " + composite.getName() + " 不存在，请先创建再进行添加操作！");
						}
					} else {
						textArea.appendText("团队2 " + composite2.getName() + " 不存在，请先创建再进行添加操作！");
					}

				} else {
					textArea.appendText("请输入两个不同的团队再进行添加操作！");
				}
			}
		});

		Button removePersonButton = new Button("删除个人");
		removePersonButton.setPrefSize(150, 25);
		removePersonButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				SafeLeaf leaf = new SafeLeaf();
				leaf.setName(personTextField.getText());
				Iterator<SafeComponent> iterator = safeLeaf.iterator();
				while (iterator.hasNext()) {
					SafeLeaf temp = (SafeLeaf) iterator.next();
					if (temp.getName().equals(leaf.getName())) {
						leaf = temp;
						break;
					}
				}

				if (safeLeaf.contains(leaf)) {
					safeLeaf.remove(leaf);

					SafeComposite temp = null;
					for (SafeComponent c : safeComposite) {
						if (leaf.getParent() != null
								&& leaf.getParent().getName().equals(((SafeComposite) c).getName())) {
							temp = (SafeComposite) c;
							safeComposite.remove(temp);
							temp.removeChild(leaf);
							safeComposite.add(temp);
							break;
						}
					}

					textArea.appendText("个人 " + leaf.getName() + " 删除成功！");
				} else {
					textArea.appendText("个人 " + leaf.getName() + " 不存在，无法删除！");
				}
			}
		});

		Button removeTeamButton = new Button("删除团队1");
		removeTeamButton.setPrefSize(150, 25);
		removeTeamButton.setOnAction(new EventHandler<ActionEvent>() {
			public void handle(ActionEvent event) {
				textArea.setText("");

				SafeComposite composite = new SafeComposite();
				composite.setName(teamTextField.getText());
				Iterator<SafeComponent> iterator = safeComposite.iterator();
				while (iterator.hasNext()) {
					SafeComposite temp = (SafeComposite) iterator.next();
					if (temp.getName().equals(composite.getName())) {
						composite = temp;
						break;
					}
				}

				if (safeComposite.contains(composite)) {
					if (composite.getParent() != null) {
						SafeComposite parent = new SafeComposite();
						parent.setName(composite.getParent().getName());
						Iterator<SafeComponent> iterator2 = safeComposite.iterator();
						while (iterator2.hasNext()) {
							SafeComposite temp = (SafeComposite) iterator2.next();
							if (temp.getName().equals(parent.getName())) {
								parent = temp;
								break;
							}
						}

						safeComposite.remove(parent);
						parent.removeChild(composite);
						safeComposite.add(parent);
					}

					removeComposite(composite);
					textArea.appendText("团队1 " + composite.getName() + " 删除成功！");
				} else {
					textArea.appendText("团队1  " + composite.getName() + " 不存在，无法删除！");
				}
			}
		});

		HBox buttonHBox = new HBox(5);
		buttonHBox.setPadding(new Insets(5, 0, 0, 0));
		buttonHBox.getChildren().addAll(viewAllButton, createPersonButton, createTeamButton);

		HBox buttonHBox2 = new HBox(5);
		buttonHBox2.setPadding(new Insets(0, 0, 5, 0));
		buttonHBox2.getChildren().addAll(addPersonToTeamButton, addTeamToTeamButton, removePersonButton,
				removeTeamButton);

		vBox.getChildren().addAll(personHBox, teamHBox, team2HBox, textArea, buttonHBox, buttonHBox2);
		Scene scene = new Scene(vBox, 600, 400);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	public void viewComposite(SafeComponent c, int level) {
		String preStr = "";
		for (int i = 0; i < level; i++) {
			preStr += "    ";
		}

		if (level == 1) {
			textArea.appendText("+团队：" + ((SafeComposite) c).getName() + "\n");
		}

		if (((SafeComposite) c).getChildren() != null) {
			for (SafeComponent child : ((SafeComposite) c).getChildren()) {
				if (child != null) {
					if (child instanceof SafeComposite) {
						textArea.appendText(preStr + "+团队：" + ((SafeComposite) child).getName() + "\n");
						viewComposite(((SafeComposite) child), level + 1);
					} else {
						textArea.appendText(preStr + "-个人：" + ((SafeLeaf) child).getName() + "\n");
					}
				}
			}
		}
	}

	public void removeComposite(SafeComponent c) {
		if (((SafeComposite) c).getChildren() != null) {
			for (SafeComponent child : ((SafeComposite) c).getChildren()) {
				if (child != null) {
					if (child instanceof SafeComposite) {
						removeComposite(((SafeComposite) child));
					} else {
						safeLeaf.remove((SafeLeaf) child);
					}
				}
			}
		}

		safeComposite.remove(c);
	}
}
